<template>
  <div>
    <div class="gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>考试名称</p>
          <el-input v-model="qo.Filter" placeholder="请输入考试名称" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="4">
          <p>发布机构</p>
          <el-input v-model="qo.PublishUnitName" placeholder="请输入发布机构" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="6">
          <p>考试时间</p>
          <el-date-picker
            v-model="examTime"
            size="mini"
            type="daterange"
            style="width:100%"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleExamTimeChange"
          />
        </el-col>
        <el-col :span="6">
          <p>邀请时间</p>
          <el-date-picker
            v-model="inviteTime"
            size="mini"
            type="daterange"
            style="width:100%"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleInviteTimeChange"
          />
        </el-col>
        <el-col :span="4">
          <p>邀请状态</p>
          <el-select v-model="StatusCode" size="mini" placeholder="请选择邀请状态" filterable @change="getPageList()">
            <el-option :value="null" label="全部" />
            <el-option v-for="item in dict.duanExamParticipatingUnitStatus" :key="item.code" :value="item.code" :label="item.value" />
          </el-select>
        </el-col>
        <el-col :span="4" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="reset()">重置</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" class="gl_table_noBorder" :data="tableData" size="mini">
      <el-table-column label="序号" type="index" />
      <el-table-column label="考试名称" prop="duanExamName" :min-width="120" />
      <el-table-column label="发布机构" prop="organizationUnitName" :min-width="150" />
      <el-table-column label="考试级别" prop="duanNames" :min-width="150">
        <template slot-scope="{row}">
          <span v-for="(item, index) in row.duanNames" :key="item">{{ item }} {{ row.duanNames.length - 1 === index ? '': '，' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试时间" min-width="170">
        <template slot-scope="{row}">
          <span v-if="row.examStartTime">
            {{ row.examStartTime | FormatDate('yyyy-MM-dd') }} 至 {{ row.examEndTime | FormatDate('yyyy-MM-dd') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="邀请时间" min-width="135">
        <template slot-scope="{row}">
          <span v-if="row.creationTime">
            {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="邀请状态" min-width="100">
        <template slot-scope="{row}">
          <span v-for="item in dict.duanExamParticipatingUnitStatus" :key="item.code">
            {{ item.code === row.statusCode ? item.value : '' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" :disabled="row.statusCode !== 'Pending'" @click="clickPoint('Accept', row.id)">同意</el-button>
          <el-button size="mini" type="text" :disabled="row.statusCode !== 'Pending'" @click="clickPoint('Refused', row.id)">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row class="gl_mt_15" style="text-align: right;">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="qo.MaxResultCount"
        :current-page="qo.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import {
  Api_GET_ClubDuanExamParticipatingUnitInvitorsQO // 获取受邀列表
} from '@/lib/form/hall/swaggerForm';
import {
  api_GET_ClubDuanExamParticipatingUnitInvitors, // 获取受邀列表
  api_PUT_ClubDuanExamParticipatingUnitIdInvitationStatus // 更新邀请状态
} from '@/extend/api/hallswaggerApi/app';
export default {
  data () {
    return {
      qo: new Api_GET_ClubDuanExamParticipatingUnitInvitorsQO(),
      tableData: [],
      loading: false,
      total: 0,
      inviteTime: [],
      examTime: [],
      StatusCode: null
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict
    })
  },
  created() {
    this.getDict();
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict' }),
    getPageList(page) {
      this.qo.Page = page || 1;
      this.qo.SkipCount = (this.qo.Page - 1) * this.qo.MaxResultCount;
      this.qo.IncludeRequest = true;
      this.qo.StatusCodes = this.StatusCode;
      this.qo.IncludeDuan = true;
      this.qo.Sorting = 'creationTime DESC';
      // this.qo.IncludeDuanExam = true;
      // this.qo.IncludeRequestDuans = true;
      this.loading = true;
      api_GET_ClubDuanExamParticipatingUnitInvitors(this.qo).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    reset() {
      this.qo = new Api_GET_ClubDuanExamParticipatingUnitInvitorsQO();
      this.examTime = [];
      this.inviteTime = [];
      this.StatusCode = null;
      this.getPageList();
    },
    // 撤销
    clickPoint(type, id) {
      const choose = type ? '同意' : '拒绝';
      this.$confirm(`您确定要${choose}该项申报吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // Accept Refused Pending
        this.tableLoading = true;
        api_PUT_ClubDuanExamParticipatingUnitIdInvitationStatus(id, { statusCode: type }).then(() => {
          this.getPageList();
          this.$message({ type: 'success', message: '操作成功！' });
        }, () => {
          this.tableLoading = false;
          this.$message({ type: 'error', message: '操作失败！' + error.error.message });
        });
      }).catch(() => {});
    },
    handleExamTimeChange(time) {
      this.qo.ExamStartDate = time ? time[0] + ' 00:00:00' : null;
      this.qo.ExamEndDate = time ? time[1] + ' 23:59:59' : null;
      this.getPageList();
    },
    handleInviteTimeChange(time) {
      this.qo.StartDate = time ? time[0] + ' 00:00:00' : null;
      this.qo.EndDate = time ? time[1] + ' 23:59:59' : null;
      this.getPageList();
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.qo.MaxResultCount = pagesize;
      this.getPageList();
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
